package com.game.service.windows;

import com.game.service.define.RoomConfig;
import com.game.service.socket.FileUtils;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;

public class LogWindows {
    private static final int MAX_LENGTH = 500000;
    private final JTextArea textArea;
    private static LogWindows logWindows;

    public LogWindows() {
        // 创建主窗口
        JFrame frame = new JFrame("日志窗口" + RoomConfig.SERVER_NAME + RoomConfig.Version);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLayout(new BorderLayout());

        // 创建文本区域用于显示输出
        textArea = new JTextArea();
        textArea.setEditable(false);
        Font font = new Font("", Font.PLAIN, 20); // 字体类型、样式和大小
        textArea.setFont(font);
        JScrollPane scrollPane = new JScrollPane(textArea);
        // 将文本区域添加到窗口
        frame.add(scrollPane, BorderLayout.CENTER);
        // 显示窗口
        frame.setVisible(true);
    }

    public void printToTextArea(String message, Class<?> tClass) {
        String txt = FileUtils.getTime() + message + "\n";
        textArea.append(txt);
        LoggerFactory.getLogger(tClass).info(message);
        String content = textArea.getText();
        if (content.length() > MAX_LENGTH) {
            String temp = content.substring(MAX_LENGTH / 2, MAX_LENGTH);
            textArea.setText(temp);
            System.err.println(temp.length());//设置最大显示,超过时截取后一半显示,想看全部时到硬盘位置查看
        }

    }

    public static LogWindows getLogWindows() {
        if (logWindows == null) {
            logWindows = new LogWindows();
        }
        return logWindows;
    }

    public void clearTextArea() {
        textArea.setText("");
    }
}